package com.woniuxy.book.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.book.entity.OrderItem;
import com.woniuxy.book.entity.PageInfo;
import com.woniuxy.book.mapper.OrderItemMapper;
import com.woniuxy.book.service.OrderItemService;
import com.woniuxy.book.service.dto.SelectOrderItemDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author li
 * @since 2024年2月09日
 */
@Service
public class OrderItemServiceImpl implements OrderItemService {

    @Resource
    private OrderItemMapper orderItemMapper;
    @Override
    public SelectOrderItemDTO selectOrderItem(Integer pageNum, Integer pageSize, String orderId) {
        QueryWrapper<OrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        Page<OrderItem> page = new Page<OrderItem>(pageNum == null ? 1 : pageNum, pageSize == null ? 5 : pageSize);
        orderItemMapper.selectPage(page, wrapper);
        Integer count = 0;
        for (OrderItem orderItem : page.getRecords()) {
            count+=orderItem.getBookCont();
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(page.getRecords());
        SelectOrderItemDTO selectOrderItemDTO = new SelectOrderItemDTO();
        selectOrderItemDTO.setCount(count);
        selectOrderItemDTO.setPageInfo(pageInfo);
        return selectOrderItemDTO;
    }
}
